import Vue from 'vue'
import VueRouter from 'vue-router'
import Bossman from "@/views/Bossman/index.vue";
import store from "@/store";

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/index.vue')
  },
  {
    path: '/enroll',
    name: 'Enroll',
    component: () => import('@/views/Suppliers/Enroll/index.vue')
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/index.vue')
  }

]

const router = new VueRouter({
  mode: 'history',
  base: '/api',
  routes
})

//提供一个重置路由的方法
export const resetRouter = () => {
  router.matcher = new VueRouter({ mode: 'history', base: '/api' })
}

//注意，刷新界面会导致页面路由重置
export const setRoutes = () => {
  const storeMenus = localStorage.getItem('menus');
  if (storeMenus) {

    //获取当前的路由对象名称数组
    const currentRouteNames = router.getRoutes().map(v => v.name)
    if (!currentRouteNames.includes('Bossman')) {
      //拼装动态路由
    const manageRoute = {
      path: '/',
      name: 'Bossman',
      component: () => import('@/views/Bossman/index.vue'),
      redirect: '/bossmanhome',
      children: [
          { path: '/userInfo', name: '个人信息', component: () => import('@/views/Bossman/UserInfo/index.vue') },
          { path: '/password', name: '修改密码', component: () => import('@/views/Bossman/Password/index.vue') },
      ]
    }
    const menus = JSON.parse(storeMenus)
    //处理一级菜单
    menus.forEach(item => {
      if (item.menuUrl) { //当menuUrl不为空的时候采取设置路由
        let itemMenu = {
          path: item.menuUrl.replace('/', ''),
          name: item.menuName,
          component: () => import(`@/views/${item.pagePath}/index.vue`)
        }
        manageRoute.children.push(itemMenu)
      }
      //处理二级菜单
      // 处理二级菜单
      if (item.children && item.children.length) {
        item.children.forEach(subItem => {
          if (subItem.menuUrl) {
            const subItemMenu = {
              path: subItem.menuUrl.replace('/', ''),
              name: subItem.menuName,
              component: () => import(`@/views/${subItem.pagePath}/index.vue`)
            };
            manageRoute.children.push(subItemMenu);
          }

          // 处理三级菜单
          if (subItem.children) {
            subItem.children.forEach(thirdItem => {
              if (thirdItem.menuUrl) {
                const thirdItemMenu = {
                  path: thirdItem.menuUrl.replace('/', ''),
                  name: thirdItem.menuName,
                  component: () => import(`@/views/${thirdItem.pagePath}/index.vue`)
                };
                manageRoute.children.push(thirdItemMenu);
              }
            });
          }
        });
      }
    })
      //动态添加到现在的路由对象里去
      router.addRoute(manageRoute)
    }

  }
}

//重置我就再set一次路由
setRoutes()

// 路由拦截器(路由守卫)
router.beforeEach((to, from, next) => {
  // 动态更新页面标题
  if (to.meta && to.meta.title) {
    document.title = to.meta.title;
  }

  // 保存当前路径名到 localStorage
  localStorage.setItem('currentPathName', to.name)
  // 通过 store 更新路径信息
  store.commit('setPath')

  if (!to.matched.length) {
    const storeMenus = localStorage.getItem('menus');
    if (storeMenus) {
      next("/404");
    } else {
        next('/login');
    }
  }
  //其他情况放行
  next()


})


export default router
